﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace LitTools
{
    /// <summary>
    /// 消息管理
    /// </summary>
    public partial class MessageManager : Singleton<MessageManager>, ISingleton
    {
        int ISingleton.Order => 1;
        //消息字典
        private Dictionary<string, Action<object[]>> messageDict;

        void ISingleton.OnInit()
        {
            messageDict = new Dictionary<string, Action<object[]>>();
        }

        void ISingleton.OnUpdate()
        {

        }
        void ISingleton.OnDestroy()
        {

        }
    }

    public partial class MessageManager
    {
        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        public void SendMessage(string message, params object[] args)
        {
            //消息字典
            if (messageDict.ContainsKey(message))
            {
                messageDict[message]?.Invoke(args);
            }
            else
            {
                Debug.Log($"[MessageManager] messageDict中不包含{message}消息");
            }
        }

        /// <summary>
        /// 注册消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="act"></param>
        public void RegisterMessage(string message, Action<object[]> act)
        {
            if (!messageDict.ContainsKey(message))
                messageDict.Add(message, null);
            messageDict[message] += act;
        }

        /// <summary>
        /// 取消注册消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="act"></param>
        public void UnregisterMessage(string message, Action<object[]> act)
        {
            if (!messageDict.ContainsKey(message))
            {
                Debug.LogWarning($"[MessageManager] messageDict中不包含{message}消息");
                return;
            }
            messageDict[message] -= act;
        }
    }
}